﻿using ERP.Application.Contracts.ERPSystem.ProductionSystem.Dto;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 生产系统-设备管理-点检保养计划-xzc
    /// </summary>
    public class MaintenanceSerivces:ApplicationService, IMaintenanceSerivces
    {
        private readonly IRepository<Maintenance> _maintenanceRepository;

        public MaintenanceSerivces(IRepository<Maintenance> maintenanceRepository)
        {
            _maintenanceRepository = maintenanceRepository;
        }
        /// <summary>
        /// 新增点检保养计划
        /// </summary>
        /// <param name="maintenancedto"></param>
        /// <returns></returns>
        public async Task<int> AddMaintenance(MaintenanceDto maintenancedto)
        {
            var maintenanceList = ObjectMapper.Map<MaintenanceDto, Maintenance>(maintenancedto);
            var i = await _maintenanceRepository.InsertAsync(maintenanceList);
            return i == null ? 0 : 1;
        }
        /// <summary>
        /// 更改点检保养计划
        /// </summary>
        /// <param name="maintenancedto"></param>
        /// <returns></returns>
        public async Task<int> UpdateMaintenance(MaintenanceDto maintenancedto)
        {
            var maintenanceList = ObjectMapper.Map<MaintenanceDto, Maintenance>(maintenancedto);
            var result = await _maintenanceRepository.UpdateAsync(maintenanceList);
            return result == null ? 0 : 1;
        }
        /// <summary>
        /// 通过id查询到对应的点检保养计划
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<Maintenance>> FindMaintenance(int id)
        {
            var maintenanceList = await _maintenanceRepository.GetListAsync(m=>m.Id == id);
            return maintenanceList;
        }
        /// <summary>
        /// 删除点检保养计划
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteMaintenance(int id)
        {
            await _maintenanceRepository.DeleteAsync(m => m.Id == id);
        }
        /// <summary>
        /// 批量删除点检保养计划
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task BatchDeleteMaintenance(List<int> ids)
        {
            foreach(var id in ids)
            {
                await _maintenanceRepository.DeleteAsync(m => m.Id == id);
            }
        }
        /// <summary>
        /// 查询点检保养计划
        /// </summary>
        /// <param name="getmaintenancedto"></param>
        /// <returns></returns>
        public async Task<List<Maintenance>> GetMaintenance(GetMaintenanceDto getmaintenancedto)
        {
            var maintenanceList = await _maintenanceRepository.GetListAsync();
            maintenanceList = maintenanceList.WhereIf(!string.IsNullOrEmpty(getmaintenancedto.Plancode),m=>m.Plancode == getmaintenancedto.Plancode).ToList();
            maintenanceList = maintenanceList.WhereIf(!string.IsNullOrEmpty(getmaintenancedto.PlanType), m => m.PlanType == getmaintenancedto.PlanType).ToList();
            maintenanceList = maintenanceList.WhereIf(!string.IsNullOrEmpty(getmaintenancedto.PlanName), m => m.PlanName.Contains(getmaintenancedto.PlanName)).ToList();
            maintenanceList = maintenanceList.WhereIf(!string.IsNullOrEmpty(getmaintenancedto.Status), m => m.Status == getmaintenancedto.Status).ToList();
            return maintenanceList;
        }
    }
}
